<template>
  <a-layout-header class="header">
    <div class="header-content">
      <div class="logo">
        <router-link to="/">
          <h1>技术论坛</h1>
        </router-link>
      </div>
      
      <div class="nav-menu">
        <router-link to="/tech" class="nav-item">技术讨论</router-link>
        <router-link to="/life" class="nav-item">生活分享</router-link>
        <router-link to="/chat" class="nav-item">聊天室</router-link>
      </div>

      <div class="search-box">
        <a-input-search
          v-model:value="searchText"
          placeholder="搜索帖子、评论或用户"
          @search="onSearch"
          style="width: 250px"
        >
          <template #prefix>
            <search-outlined />
          </template>
        </a-input-search>
      </div>

      <div class="user-section">
        <template v-if="userStore.isLoggedIn">
          <a-dropdown>
            <div class="user-avatar">
              <a-avatar :size="32">
                <template v-if="userStore.userInfo.picture">
                  <img :src="userStore.userInfo.picture" alt="avatar" />
                </template>
                <template v-else>
                  <user-outlined />
                </template>
              </a-avatar>
              <span class="username">{{ userStore.userInfo.username }}</span>
            </div>
            <template #overlay>
              <a-menu>
                <a-menu-item key="profile">
                  <router-link to="/profile">
                    <user-outlined /> 个人中心
                  </router-link>
                </a-menu-item>
                <a-menu-item key="messages">
                  <router-link to="/messages">
                    <message-outlined /> 消息中心
                  </router-link>
                </a-menu-item>
                <a-menu-item key="favorites">
                  <router-link to="/favorites">
                    <star-outlined /> 我的收藏
                  </router-link>
                </a-menu-item>
                <a-menu-item key="posts">
                  <router-link to="/my-posts">
                    <file-text-outlined /> 我的帖子
                  </router-link>
                </a-menu-item>
                <a-menu-item key="admin">
                  <router-link to="/admin/post-audit">
                    <crown-outlined /> 管理后台
                  </router-link>
                </a-menu-item>
                <a-menu-divider />
                <a-menu-item key="logout" @click="handleLogout">
                  <logout-outlined /> 退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </template>
        <template v-else>
          <router-link to="/login" class="nav-item">登录</router-link>
          <router-link to="/register" class="nav-item">注册</router-link>
        </template>
      </div>
    </div>
  </a-layout-header>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { 
  UserOutlined, 
  BellOutlined, 
  MessageOutlined, 
  StarOutlined,
  LogoutOutlined,
  FileTextOutlined,
  SearchOutlined,
  CrownOutlined
} from '@ant-design/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const searchText = ref('')
const selectedKeys = ref(['home'])

// 模拟未读消息数量
const unreadCount = ref(5)

const goToLogin = () => {
  router.push('/login')
}

const goToRegister = () => {
  router.push('/register')
}

const goToMessages = () => {
  router.push('/messages')
}

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}

const onSearch = (value) => {
  if (value.trim()) {
    router.push({
      path: '/search',
      query: { q: value.trim() }
    })
  }
}
</script>

<style lang="scss" scoped>
.header {
  background: #fff;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: fixed;
  width: 100%;
  z-index: 1000;

  .header-content {
    max-width: 1200px;
    margin: 0 auto;
    height: 100%;
    display: flex;
    align-items: center;
    padding: 0 24px;

    .logo {
      h1 {
        margin: 0;
        font-size: 20px;
        color: #1890ff;
      }
    }

    .nav-menu {
      margin-left: 48px;
      flex: 1;

      .nav-item {
        color: rgba(0, 0, 0, 0.65);
        padding: 0 20px;
        font-size: 14px;
        transition: color 0.3s;

        &:hover {
          color: #1890ff;
        }

        &.router-link-active {
          color: #1890ff;
        }
      }
    }

    .search-box {
      margin-left: 48px;
      flex: 1;
    }

    .user-section {
      display: flex;
      align-items: center;
      gap: 16px;

      .user-avatar {
        display: flex;
        align-items: center;
        gap: 8px;
        cursor: pointer;
        padding: 4px 8px;
        border-radius: 4px;
        transition: background-color 0.3s;

        &:hover {
          background-color: rgba(0, 0, 0, 0.025);
        }

        .username {
          color: rgba(0, 0, 0, 0.85);
          font-size: 14px;
        }

        :deep(.ant-avatar) {
          background-color: #1890ff;
          
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
      }
    }
  }
}
</style> 